import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [
  {
    path:'/',
    name:'home',
    redirect:{name:'login'},
    component:Home
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login.vue')
  },
  {
    path: '/sign',
    name: 'sign',
    component: () => import('../views/sign.vue')
  },
  {
    path:'/about',
    name:'about',
    text: "系统首页",
    icon:"el-icon-s-home",
    redirect:{name:'index'},
    component: () => import('../views/About.vue'),
    children: [
      {
        path: 'index',
        name: 'index',
        text: "首页",
        icon:'el-icon-s-promotion',
        component: () => import('../views/index.vue')
      }
    ]
  },
  {
    path:'/about',
    name:'about',
    text:'商品管理',
    icon:"el-icon-s-grid",
    component: () => import('../views/About.vue'),
    children:[
      {
        path:'shopstate',
        name:'shopstate',
        text:'商品状态',
        icon:'el-icon-document-copy',
        component:() => import("../views/controlshop/shopstate.vue")
      },
      {
        path:'addshop',
        name:'addshop',
        text:'添加商品',
        icon:'el-icon-circle-plus-outline',
        component:() => import("../views/controlshop/addshop.vue")
      }
    ]
  },
  {
    path:'/about',
    name:'about',
    text:'轮播图',
    icon:"el-icon-picture",
    component: () => import('../views/About.vue'),
    children:[
      {
        path:'swiper',
        name:'swiper',
        text:'轮播图',
        icon:'el-icon-picture-outline-round',
        component:() => import("../views/swiper/swiper.vue")
      }
    ]
  },
  {
    path:'/about',
    name:'about',
    text:'物流管理',
    icon:"el-icon-truck",
    component: () => import('../views/About.vue'),
    children:[
      {
        path:'logistics',
        name:'logistics',
        text:'物流管理',
        icon:'el-icon-s-operation',
        component:() => import("../views/logistics/logistics.vue")
      }
    ]
  },
  {
    path:'/about',
    name:'about',
    text:'修改头像',
    icon:"el-icon-s-custom",
    component: () => import('../views/About.vue'),
    children:[
      {
        path:'metidea',
        name:'metidea',
        text:'修改头像',
        icon:'el-icon-edit',
        component:() => import("../views/metidea/metidea.vue")
      }
    ]
  },
]

const router = new VueRouter({
  routes
})


router.beforeEach((to,from,next)=>{
  if(to.path.startsWith("/sign")){
    next()
  }else if(to.path.startsWith("/login")){
    window.sessionStorage.removeItem("userInfo")
    next()
  }else{
    let user = window.sessionStorage.getItem("userInfo")
    if(!user){
      next({path:'/login'})
    }else{
      next()
    }
  }
})
export default router